import { getuserlist,usercount } from "@/request/api";
interface memberState{
    size: any;
    page: any;
    count: any;
    userList:Array<object>
}
const state = {
    userList:[],
    page:1,
    size:2,
    count:0
};
const getters = {
    getUserList(state:memberState){
        return state.userList
    },
    getSize(state:memberState){
        return state.size
    },
    getCount(state:memberState){
        return state.count
    },
    getPage(state:memberState){
        return state.page
    }
};
const mutations = {
    REQ_USERLIST(state:memberState,payload:any){
        state.userList = payload
    },
    CHANGE_PAGE(state:memberState,payload:any){
        state.page = payload
    },
    REQ_COUNT(state:memberState,payload:any){
        state.count = payload
    },
};
const actions = {
    getUserListAction(context:any){
        console.log(context,'11111111');
        getuserlist({
            size:context.state.size,
            page:context.state.page,
        }).then((res:any)=>{
            let list = res.list ? res.list:[]
            if (context.state.page != 1 && list.length == 0) {
                context.dispatch('changPageAction',context.state.page-1)
            }
            context.commit('REQ_USERLIST',res.list)
        })
    },
    getCountAction({commit}:any){
        usercount().then((res:any)=>{
            commit('REQ_COUNT',res.list[0].total)
        })
    },
    changPageAction(context:any,payload:any){
        context.commit('CHANGE_PAGE',payload);
        context.dispatch('getUserListAction')
    }
};
export default{
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}